package com.softstao.guoyu.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.softstao.guoyu.R;
import com.softstao.guoyu.model.shop.Goods;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewBaseAdapter;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewHolder;
import com.softstao.softstaolibrary.library.utils.LZUtils;
import com.softstao.softstaolibrary.library.widget.NumberView;

import java.util.List;

/**
 * Created by vaikus on 2017/7/25.
 */

public class ChooseProductAdapter extends RecycleViewBaseAdapter<Goods> {
    private GoodsListener goodsListener;
    public ChooseProductAdapter(List<Goods> datas) {
        super(datas, R.layout.layout_goods_item);
    }

    @Override
    public void convert(RecycleViewHolder holder, Goods goods) {
        RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        holder.itemView.setLayoutParams(layoutParams);
        if (goods.getImg()!=null && !goods.getImg().equals("")){
            Glide.with(mContext).load(goods.getImg()).into((ImageView) holder.getView(R.id.img));
        }else {
            ((ImageView) holder.getView(R.id.img)).setImageResource(R.mipmap.default_im);
        }
        holder.setText(R.id.name,goods.getName())
                .setText(R.id.price,mContext.getResources().getString(R.string.rmb) + LZUtils.priceFormat(goods.getPrice()))
                .setText(R.id.box_num,"订单基数：" + goods.getBoxNum() + "/箱");
        NumberView numberView = holder.getView(R.id.goods_num);
        numberView.setNumber(0,0);
        numberView.setListener(new NumberView.OnNumberButtonClickListener() {
            @Override
            public void onDecreaseButtonClick(View view, int number, int preNumber) {
                if (goodsListener!=null){
                    goodsListener.subtractPrice(holder.getAdapterPosition(),preNumber,number);
                }

            }

            @Override
            public void onIncreaseButtonClick(View view, int number) {
                if (goodsListener != null){
                    goodsListener.addPrice(holder.getAdapterPosition());
                }
                numberView.setNumber(number,1);
            }
        });
    }

    public interface GoodsListener{
        void subtractPrice(int position,int preNumber, int number);
        void addPrice(int position);
    }

    public GoodsListener getGoodsListener() {
        return goodsListener;
    }

    public void setGoodsListener(GoodsListener goodsListener) {
        this.goodsListener = goodsListener;
    }
}
